package cn.iocoder.yudao.module.contract.service.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBrokerageDO;
import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicBrokerageMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPackingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicPackingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 包装方式 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BasicPackingServiceImpl extends ServiceImpl<BasicPackingMapper, BasicPackingDO> implements BasicPackingService {

    @Resource
    private BasicPackingMapper basicPackingMapper;

    @Override
    public Long createBasicPacking(BasicPackingSaveReqVO createReqVO) {
        // 插入
        BasicPackingDO basicPacking = BeanUtils.toBean(createReqVO, BasicPackingDO.class);
        validateUnique(basicPacking);
        basicPackingMapper.insert(basicPacking);
        // 返回
        return basicPacking.getId();
    }

    @Override
    public void updateBasicPacking(BasicPackingSaveReqVO updateReqVO) {
        // 校验存在
        validateBasicPackingExists(updateReqVO.getId());
        // 更新
        BasicPackingDO updateObj = BeanUtils.toBean(updateReqVO, BasicPackingDO.class);
        validateUnique(updateObj);
        basicPackingMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicPacking(Long id) {
        // 校验存在
        validateBasicPackingExists(id);
        // 删除
        basicPackingMapper.deleteById(id);
    }

    private void validateBasicPackingExists(Long id) {
        if (basicPackingMapper.selectById(id) == null) {
            throw exception(BASIC_PACKING_NOT_EXISTS);
        }
    }

    @VisibleForTesting
    void validateUnique(BasicPackingDO data) {
        long count = basicPackingMapper.selectCount(new LambdaQueryWrapper<BasicPackingDO>().eq(BasicPackingDO::getCode,data.getCode()).ne(data.getId()!=null,BasicPackingDO::getId,data.getId()));
        // 如果 id 为空，说明不用比较是否为相同 id 的用户
        if (count>0 ) {
            throw exception("包装方式编码已存在");
        }
    }

    @Override
    public BasicPackingDO getBasicPacking(Long id) {
        return basicPackingMapper.selectById(id);
    }

    @Override
    public PageResult<BasicPackingDO> getBasicPackingPage(BasicPackingPageReqVO pageReqVO) {
        return basicPackingMapper.selectPage(pageReqVO);
    }

}